﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZMCL.UI.PopWindows
{
    public partial class RiskRecheck : EFViewCmes
    {
        public RiskRecheck(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    this.fdRecheckDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                    this.fdIsRedo.Value = "否";
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick("ZMCL70", "recheck", sender, e);
        }

        /// <summary>
        /// 校验前的工作
        /// </summary>
        /// <returns></returns>
        public override bool ThisValidateField()
        {
            string _value = this.fdIsRedo.Value == null ? "" : this.fdIsRedo.Value.ToString();
            if (_value.Equals("是"))
            {
                this.fdRedoSuggestion.eRegex = "^.{1,500}$";
                this.fdRedoSuggestion.eValidateErrorPrompt = "输入1~500个字符";
            }
            else
            {
                this.fdRedoSuggestion.eRegex = null;
            }
            return true;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        #region 页面操作

        /// <summary>
        /// 复查人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdRecheckPeopleId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            CmesCustomView custom = (sender as EFDataField).Content as CmesCustomView;
            if (custom.ItemObject != null && !"".Equals(custom.ItemObject))
            {
                this.fdRecheckPeople.Value = custom.ItemObject.GetType().GetProperty("empName").GetValue(custom.ItemObject, null);
            }
        }

        /// <summary>
        /// 是否重新整改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdIsRedo_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (null == e.NewValue)
            {
                return;
            }

            string fdIsRedo = e.NewValue.ToString();
            if ("是".Equals(fdIsRedo))
            {
                this.fdRedoSuggestion.Visibility = Visibility.Visible;
            }
            else
            {
                this.fdRedoSuggestion.Visibility = Visibility.Collapsed;
            }
        }

        #endregion

    }
}

